<?php
    /* functions */
    function serviceZozo($data)
    {
        $url = "http://partner.mvdigital.vn/taoviec/moreceiver";    
        
        $post = array();
        $post["phone"] = trim($data["sender"]);
        $post["message"] = trim($data["content"]);
        $post["serviceid"] = trim($data["serviceCode"]);
        $post["servicecode"] = "TVZO";
        $post["transid"] = trim($data["transactionId"]);
        $post["user"] = "zozo_taoviec";
        $post["pass"] = "mZpGcdeP";
        
        $curl_connection = curl_init($url);
         
        //set options
        curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
         
        //set data to be posted
        curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post);
        
        $result = curl_exec($curl_connection);        
        $result = explode("|", $result);
        return $result;
    }
    
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);
        $refCode = !empty($match[2]) ? $match[2] : "";
        $username = !empty($match[5]) ? $match[5] : "";        
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["user_name"] = !empty($user) ? $user["username"] : "";
        $result["username"] = $username;        
            
        return $result;
    }
    
    /*
    * processing
    */
    
    function processing($data)
    {
        $regx_nap = "/^TVA GAME TVZO(\s+)([a-zA-Z0-9_]{1,})(\s+)NAP(\s+)([a-zA-Z0-9_.]{1,})$/si";                
        $data_reg = array();
        if(preg_match($regx_nap, $data["content"])){
            $syntax = buildSyntax($data["content"], $regx_nap);
            $list_service_code = array("6522", "6622", "6722");            
            
            $data_reg["game_user"] = isset($syntax["username"]) ? $syntax["username"] : "";
            $data_reg["username"] = isset($syntax["user_name"]) ? $syntax["user_name"] : "";
            
            $data["userId"] = $syntax["userId"]; 
            if(in_array($data["serviceCode"], $list_service_code)){                            
                $result = serviceZozo($data);                                        
                
                
                $response["status"] = $result[0];
                $response["msg"] = $result[1];
                
                $data["status"] = $result[0];                                   
                                
            } else {
                $response["status"] = 3;                        
                $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                
                $data["status"] = 0;        
            }               
                
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
            
            $data["userId"] = 0;                
            $data["status"] = 0;    
        }
        
        /* log sms */
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 52; //ID partner mvcorp    
        $data["type"] = 1; //Thể loại game   
        $data["id"] = 175; //ID game zozo        
        $data["output"] = $response["status"] . "|" . $response["res_status"] . "|" . $data["transactionId"] . "|" . $response["msg"];    
        
        /* tỷ lệ ăn chia */
        $percent = getPercent($data["id"]);
        
        $data["percent_wapmaster"] = isset($percent["percent_wapmaster"]) ? $percent["percent_wapmaster"] : 0;
        $data["percent_partner"] = isset($percent["percent_partner"]) ? $percent["percent_partner"] : 0;
        $data["action"] = "reg"; //action
        
        $result = InsertSmsLog($data);   
        
        if($data["action"]=="reg"){
            $data_reg["userId"] = $data["userId"];    
            $data_reg["gameId"] = $data["id"];                
            
            /* Check xem user da ton tai chua */
            $result = CheckGameUser($data_reg);
            if($result==0){
                $result = InsertGameUser($data_reg);    
            }            
        }
        
        return $response;
    }
?>